
WindTabsModel = function() {
	this.tabs = new Array();
}

WindTabsModel.prototype.setId = function (id) {
	this.id = id;
}

WindTabsModel.prototype.getId = function () {
	return this.id;
}

WindTabsModel.prototype.createTabModel = function() {
	var tabModel = new WindTabModel();
	this.tabs[this.tabs.length] = tabModel;
	
	if(this.getActiveTab()==null) {
		this.setActiveTab(tabModel);
	}
	return tabModel;
}

WindTabsModel.prototype.getNumberOfTabs = function() {
	return this.tabs.length;
}

WindTabsModel.prototype.getTab = function(index) {
	return this.tabs[index];
}

WindTabsModel.prototype.setActiveTab = function(newActiveTab) {
	var currentActiveTab = this.getActiveTab();
	if(newActiveTab != currentActiveTab) {
		if(currentActiveTab!=null) {
			if(currentActiveTab.getView()!=null) {
				currentActiveTab.getView().setCssClass("WindTab");
			}
		}
		
		if(newActiveTab.getView()!=null) {
			newActiveTab.getView().setCssClass("WindTabActive");
		}
		
		if(currentActiveTab!=null) {
			currentActiveTab.deactivate(); 
		}
		
		newActiveTab.activate(); 
		
		this.activeTab = newActiveTab;
	} 
}

WindTabsModel.prototype.getActiveTab = function() {
	return this.activeTab;
}

WindTabsModel.prototype.setView = function(view) {
	this.view = view;
}

WindTabsModel.prototype.getView = function() {
	return this.view;
}

WindTabModel = function() {
	this.text = "Default title";
}

WindTabModel.prototype.setId = function (id) {
	this.id = id;
}

WindTabModel.prototype.getId = function () {
	return this.id;
}

WindTabModel.prototype.setText = function(text) {
	this.text = text;
}

WindTabModel.prototype.getText = function() {
	return this.text;
}

WindTabModel.prototype.setView = function(view) {
	this.view = view;
}

WindTabModel.prototype.getView = function() {
	return this.view;
}

WindTabModel.prototype.setBody = function(body) {
	this.body = body;
}

WindTabModel.prototype.getBody = function() {
	return this.body;
}

WindTabModel.prototype.deactivate = function() {
	if(this.onDeactivation!=null) {
		this.onDeactivation();
	}
	if(this.getBody()!=null) {
		this.getBody().hide();
	}
}

WindTabModel.prototype.activate = function() {
	if(this.onActivation!=null) {
		this.onActivation();
	}
	if(this.getBody()!=null) {
		this.getBody().show();
	} 
}

WindTabModel.prototype.setURL = function(url) {
	this.url = url;
}

WindTabModel.prototype.getURL = function() {
	return this.url;
}


WindTabs = function() {
}

WindTabs.createModel = function() {
	var model = new WindTabsModel();
	return model;
}

WindTabs.createTabModel = function() {
	var model = new Object();
	
	model.getText = function() {
		return "Title";
	}	
	
	model.getView = function() {
		return model.view;
	}

	model.setView= function(view) {
		model.view = view;
	}
	
	return model;
}

WindTabs.createView = function(tabsModel) {
	var tabsView = new WindView;

	tabsView.setCssClass("WindTabs");

	if(tabsModel == null) {
		tabsModel = WindTabs.createModel();
	}

	var container = new WindView;
	container.setCssClass("WindTabsContainer");
	tabsView.addView(container);
	
	var activeTabModel = tabsModel.getActiveTab();
	
	for(var i=0; i<tabsModel.getNumberOfTabs(); i++) {
		var tabModel = tabsModel.getTab(i);
		var tabView = WindTabs.createTabView(tabsModel, tabModel);
		container.addView(tabView);
	}
	
	if(activeTabModel!=null) {
		tabsModel.setActiveTab(activeTabModel);
		activeTabModel.activate();
	}
	
	return tabsView;	
}

WindTabs.createTabView = function(tabsModel, tabModel) {
	var tabView = new WindView;
	tabModel.setView(tabView);
	tabView.setModel(tabModel);
	
	tabView.setCssClass("WindTab");
	
	if(tabModel==tabsModel.getActiveTab()) {
		tabView.setCssClass("WindTabActive");
	}
	
	tabView.onClick = function(e) {
		tabsModel.setActiveTab(tabModel);
	}
	
	tabView.setText(tabModel.getText());
	
	var url = tabModel.getURL();
	
	if(url==null) {
		url = "#";
	} else {
		url = "?" + url;
	}
	
	tabView.render = function() {
		var a = document.createElement("a");
		a.href = url;
		this.setHtmlElement(a);
		this.applyViewAttributesToHtmlElement(a);
		this.renderChildren(a);		
		return a;
	}

	return tabView;	
}